package com.phiture.erp.produce.core.factory;

import com.phiture.erp.produce.core.handler.MrpTaskDispatchHandler;
import com.phiture.erp.produce.core.pojo.dto.MrpTaskDispatchDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author YH
 * @date 2025-05-06-10:40
 */

@Component
public class MrpTaskDispatchFactory {
    private final Map<String, MrpTaskDispatchHandler> strategyMap;

//    @Autowired
//    public MrpTaskDispatchFactory(List<MrpTaskDispatchHandler> handlers) {
//        this.strategyMap = handlers.stream()
//                .collect(Collectors.toMap(
//                        handler -> handler.getClass().getAnnotation(Component.class).value(),
//                        obj -> obj
//                ));
//    }

    @Autowired
    public MrpTaskDispatchFactory(Map<String, MrpTaskDispatchHandler> strategyMap) {
        this.strategyMap = strategyMap;
    }

    public void executeTask(String strategyType, MrpTaskDispatchDTO dto) {
        getHandler(strategyType).taskExecute(dto);
    }

    public MrpTaskDispatchHandler getHandler(String strategyType) {
        MrpTaskDispatchHandler handler = strategyMap.get(strategyType);
        if (handler == null) {
            throw new IllegalArgumentException(
                    String.format("Invalid MRP strategy type: %s. Available: %s",
                            strategyType,
                            String.join(", ", strategyMap.keySet()))
            );
        }
        return handler;
    }
}
